package com.peanut.baby.mvp.main.expert.live;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.peanut.baby.InitManager;
import com.peanut.baby.R;
import com.peanut.baby.model.LiveRoom;
import com.peanut.devlibrary.imageloader.ImageLoader;
import com.peanut.devlibrary.widget.pullrecycle.BaseRecyclerAdapter;
import com.peanut.devlibrary.widget.pullrecycle.BaseViewHolder;

import java.util.List;

/**
 * Created by anonymouslystudio on 2017/7/12.
 */

public class LiveRecyclerAdapter extends BaseRecyclerAdapter<LiveRoom>{
    OnItemViewClickListener listener;

    public LiveRecyclerAdapter(Context context, List<LiveRoom> data) {
        super(context,data);
    }

    public void setOnClickListener(OnItemViewClickListener l){
        this.listener = l;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.layout_item_live_list;
    }

    @Override
    protected void convert(final BaseViewHolder holder, final LiveRoom item) {
        TextView title = holder.getView(R.id.live_title);
        TextView expertName = holder.getView(R.id.live_expert_name);
        TextView expertTitle = holder.getView(R.id.live_expert_title);
        TextView expertHospital = holder.getView(R.id.live_expert_hospital);
        TextView status = holder.getView(R.id.live_status);
        TextView time = holder.getView(R.id.live_time);
        TextView enroled = holder.getView(R.id.live_enroled);
        TextView enroleCount = holder.getView(R.id.live_enrole_count);
        ImageView image = holder.getView(R.id.live_image);
        TextView price = holder.getView(R.id.live_price);
        TextView join = holder.getView(R.id.live_join);

        title.setText(item.title);
        expertName.setText(item.expertName);
        expertHospital.setText(item.expertHospital);
        expertTitle.setText(item.expertJobTitle);
        time.setText(item.getTimeString());
        status.setText(item.getStatusString());
        enroled.setText("已报名"+item.enrolCount);
        enroleCount.setText("/"+item.allowCount);
        price.setText(item.getPriceString());

        if(item.expertId.equals(InitManager.getInstance().getUserId())
                || item.serverId.equals(InitManager.getInstance().getUserId())){
            join.setText("我的直播");
        } else {
            if(item.isEnroled == 0){
                join.setText("立即报名");
            }else{
                if(item.roomStatus == LiveRoom.ROOM_STATUS_FINISHED){
                    join.setText("回放中");
                }else{
                    join.setText("进入直播间");
                }

            }
        }


//        if(item.roomStatus == LiveRoom.ROOM_STATUS_FINISHED){
//            join.setText("回放中");
//        }else{
//
//        }

        ImageLoader.getInstance().loadImageView(title.getContext(),item.roomImageUrl,image);

        holder.getView(R.id.live_container).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onViewItemClicked(v, item);
                }
            }
        });

        join.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onViewItemClicked(v, item);
                }
            }
        });

    }

    public interface OnItemViewClickListener{
        void onViewItemClicked(View v, LiveRoom room);
    }

}
